// import 'dart:ffi';


import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:image_picker/image_picker.dart';
import 'package:images_picker/images_picker.dart';
import 'package:multi_image_picker_view/multi_image_picker_view.dart';
import 'package:qianyi/controller/homeTabController.dart';

Future<List<ImageFile>> pickImagesUsingImagePicker(bool allowMultiple) async {
  final picker = ImagePicker();
  final List<XFile> xFiles;
  if (allowMultiple) {
    xFiles = await picker.pickMultiImage(maxWidth: 1080, maxHeight: 1080);
  } else {
    xFiles = [];
    final xFile = await picker.pickImage(
        source: ImageSource.gallery, maxHeight: 1080, maxWidth: 1080);
    if (xFile != null) {
      xFiles.add(xFile);
    }
  }
  if (xFiles.isNotEmpty) {
    return xFiles.map<ImageFile>((e) => convertXFileToImageFile(e)).toList();
  }
  return [];
}

Future<List<ImageFile>> pickImagesUsingFilePicker(bool allowMultiple) async {
  const allowedExtensions = ['png', 'jpeg', 'jpg'];
  final result = await FilePicker.platform.pickFiles(
    allowMultiple: allowMultiple,
    type: FileType.custom,
    withData: kIsWeb,
    allowedExtensions: allowedExtensions,
  );
  if (result != null && result.files.isNotEmpty) {
    return result.files
        .where((e) =>
            e.extension != null &&
            allowedExtensions.contains(e.extension?.toLowerCase()))
        .map(
          (e) => ImageFile(UniqueKey().toString(),
              name: e.name,
              extension: e.extension!,
              bytes: e.bytes,
              path: !kIsWeb ? e.path : null),
        )
        .toList();
  }
  return [];
}

Future<List<ImageFile>> pickFilesUsingFilePicker(bool allowMultiple) async {
  List<Media>? res = await ImagesPicker.pick(
      count: 1,
      pickType: PickType.video,
maxTime: 1800
  );

  // FilePickerResult? result = await FilePicker.platform.pickFiles(
  //   allowMultiple: allowMultiple,
  //   // allowedExtensions: ['mp4'],
  //   type: FileType.video,
  //
  //   withData: kIsWeb,
  // );
  if (res != null && res.length > 0) {
    ImageFile imageFile = ImageFile(UniqueKey().toString(),
        name: res[0].thumbPath!,
        extension: "mp4",
        bytes: null,
        path: res[0].path
    );
    List<ImageFile> imageFileList = [imageFile];

    return imageFileList;

    /*
    the below code can be used if not using convertPlatformFileToImageFile extension.
    return result.files
        .map(
          (e) => ImageFile(UniqueKey().toString(),
              name: e.name,
              extension: e.extension!,
              bytes: e.bytes,
              path: !kIsWeb ? e.path : null),
        )
        .toList();*/
  }
  return [];
}
